<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>施工团队</title>
    <style>
    </style>
</head>
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=IENn7ABARjQIkTeAKyh80VTkg2jYXf3K"></script>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:">首页</a><span lay-separator="">/</span>
        <a><cite>系统设置</cite></a><span lay-separator="">/</span>
        <a><cite>分销设置</cite></a>
      </span>
        <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3"></i>
        </a>
</div>



<div class="layui-fluid">
    <!--<div class="layui-card">-->
    <!--</div>-->
    <form class="layui-form" action="" lay-filter="component-form-element-a" id="myform" style="margin-top: 50px">
        <div class="layui-form-item">
            <div class="layui-inline">

                <label class="layui-form-label" style="width: 180px">代理商/分销商提成比例</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="password" name="" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label" style="margin-left: -80px">%</label>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">

                <label class="layui-form-label" style="width: 180px">销售人员提成比</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="password" name="" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label" style="margin-left: -80px">%</label>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">

                <label class="layui-form-label" style="width: 180px">套装商品补助金额</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <input type="password" name="" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label" style="margin-left: -80px">元</label>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 168px;margin-top: 30px">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element-a">立即提交</button>
            </div>
        </div>
        <!--<div class="layui-inline">-->
            <!--<label class="layui-form-label">密码</label>-->
            <!--<div class="layui-input-inline" style="width: 100px;">-->
                <!--<input type="password" name="" autocomplete="off" class="layui-input">-->
            <!--</div>-->
        <!--</div>-->
    </form>


</div>

<!--弹出开设账号页面-->
<div class="layui-form" lay-filter="layuiadmin-form-useradmin"
     id="layuiadmin-form-useradmin"
     style="padding: 15px;display: none">
    <form class="layui-form" action="" lay-filter="component-form-element-a" id="myform">
        <input type="hidden" name="id">
        <input type="hidden" name="companyName" id="cname">
        <input type="hidden" name="enterprise" value="${session.om_user.enterprise.id}">
        <div class="layui-form-item">
            <label class="layui-form-label">角色选择</label>
            <div class="layui-input-inline">
                <select lay-verify="required" lay-filter="roleName" name="role_name" id="roleId">
                    <option value="">请选择</option>

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*账号</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" lay-verify="required" placeholder="请输入账号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*密码</label>
            <div class="layui-input-inline">
                <input type="text" name="passWord" lay-verify="required" placeholder="请输入密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*电话</label>
            <div class="layui-input-inline">
                <input type="text" name="telePhone" lay-verify="required" placeholder="请输入电话" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*真实姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" placeholder="请输入真实姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">*描述信息</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入描述信息" class="layui-textarea" name="remker"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">选择项目</label>
            <div class="layui-input-inline">
                <select lay-verify="required" lay-filter="area" name="proId" id="proId" required>
                    <option value="">请选择</option>

                </select>
            </div>
        </div>

        <div class="layui-form-item" id="checkBox">
            <label class="layui-form-label">可见标段</label>
            <div class="layui-input-inline" id="xxx" required>

            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element-a">立即提交</button>
            </div>
        </div>
    </form>
</div>

<div class="layui-form" lay-filter="layuiadmin-form-useradmin-a"
     id="layuiadmin-form-useradmin-a"
     style="padding: 15px;display: none">
    <form class="layui-form" action="" lay-filter="component-form-element-b" id="myform1">
        <input type="hidden" name="id">
        <input type="hidden" name="companyName" id="cname">
        <input type="hidden" name="enterprise" value="${session.om_user.enterprise.id}">
        <div class="layui-form-item">
            <label class="layui-form-label">角色选择</label>
            <div class="layui-input-inline">
                <input type="hidden" name="roleId">
                <input type="text" name="role_name" lay-verify="roleName" readonly placeholder="请输入账号" id="roleId" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*账号</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" lay-verify="required" placeholder="请输入账号" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*密码</label>
            <div class="layui-input-inline">
                <input type="text" name="passWord" lay-verify="required" placeholder="请输入密码" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*电话</label>
            <div class="layui-input-inline">
                <input type="text" name="telePhone" lay-verify="required" placeholder="请输入电话" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">*真实姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="required" placeholder="请输入真实姓名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">*描述信息</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入描述信息" class="layui-textarea" name="remker"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">选择项目</label>
            <div class="layui-input-inline">
                <select lay-filter="area1" name="proId" id="proId1">
                    <option value="">请选择</option>

                </select>
            </div>
        </div>

        <div class="layui-form-item" id="checkBox">
            <label class="layui-form-label">可见标段</label>
            <div class="layui-input-inline" id="qwe">

            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element-b">立即提交</button>
            </div>
        </div>
    </form>
</div>

<%include("/common/foot.html"){}%>
<!--标段-->
<script id="demo1" type="text/html">
    {{#  layui.each(d.list1, function(index, item1){ }}
    <div class="layui-input-inline">
        <input lay-verify="required" type="checkbox" name="areaId" value="{{ item1.id }}">{{ item1.projectName }}
    </div>
    {{#  }); }}
    {{#  if(d.list1.length === 0){ }}
    <div style="color: red;margin-top: 10px;">暂无此项目标段数据</div>
    {{#  } }}
</script>
<!--项目选择下拉框-->
<script id="demo3" type="text/html">
    <option value="" >请选择</option>
    {{#  layui.each(d.list3, function(index, item3){ }}
    <option value="{{ item3.id }}" id="opt">{{ item3.projectName }}</option>
    {{#  }); }}
    {{#  if(d.list3.length === 0){ }}
    <option value="">无数据</option>
    {{#  } }}
</script>
<script id="demo" type="text/html">
    <option value="">请选择</option>
    {{#  layui.each(d.list, function(index, item){ }}

    <option {{d.depart== item.id?"selected":""}} value="{{ item.id }}">{{ item.projectName }}</option>

    {{#  }); }}
    {{#  if(d.list.length === 0){ }}
    <option disabled value="">无</option>
    {{#  } }}
</script>
<!-- 角色选择下拉框 -->
<script id="demo2" type="text/html">
    {{#  layui.each(d.list2, function(index, item2){ }}
    <option value="{{ item2.id }}" id="qwe">{{ item2.sysRolename}}</option>
    {{#  }); }}
    {{#  if(d.list2.length === 0){ }}
    <option value="">无数据</option>
    {{#  } }}
</script>
<script>
    layui.use(['index', 'table', 'jquery','form', 'laytpl','laydate', 'layedit', 'AjaxCommon', 'AliangLayui', 'AliangCommon'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var AjaxCommon = layui.AjaxCommon;
        var AliangLayui = layui.AliangLayui;
        var laytpl=layui.laytpl;

        var title = "开设账户";
        var findAllAccount_url="system/openAccount/findAll.json";
        var openAccount_url="system/openAccount/openAccount.jhtml";
        var findPro_url="system/openAccount/getDepart.jhtml";
        var delete_url="system/openAccount/deleteById.jhtml";
        var index;//定义建立富文本编辑器全局变量
        var layedit;//定义富文本编辑器layedit全局变量
        var cols = [ //表头
            //  {field:'id',sort:'true',title:'序号',align:'center'},
            {field: 'phone', sort:'true' ,  title: '账户',align:'center'},
            {
                field:'name',sort:'true',title:'姓名',align:'center'
            },
            {title: '角色',align:'center',templet:"<div>{{d.sysRole.sysRoleCode.role_name}}</div>"},
            {field: 'companyName',  title: '企业名称',align:'center'},
            {field: 'createDate',sort:'true', title: '创建时间',align:'center',templet: "<div>{{layui.util.toDateString(d.createDate, 'yyyy-MM-dd ')}}</div>"},
            {title: '操作', fixed: 'right', width: 215, toolbar: '#table-useradmin-webuser',align:'center'}

        ];
        AliangLayui.createTable(
            "#LAY-user-manage",
            "listReload",
            "#toolbarDemo",
            title,
            'DATA/用户列表.json',
            true,
            [10, 15, 20, 25],
            ['filter', 'print', 'exports'],
            cols,
            null
        );
        $.ajax({
            url: findPro_url,
            type: "POST",
            data:{

            },
            dataType: "json",
            success: function(ret){
                var laytpl = layui.laytpl;
                var data = { //数据
                    "list3": ret
                };
                laytpl($("#demo3").html()).render(data, function (html) {
                    $("#proId").html(html);
                });
                form.render();
            },
            error:function(jqXHR, textStatus, errorThrown){

            }
        });
        var findInfo_url="system/openAccount/findByName.jhtml";
        var keyWord;
        $('#searchBtn').on('click', function () {
            keyWord = $('#search_term').val();
            if (""==keyWord||undefined==keyWord){
                window.location.reload();
            }else{
                table.reload('listReload', {
                    url: findInfo_url,
                    where: {
                        keyWord: keyWord
                    },
                    page: {
                        curr: 1//重新从第 1 页开始
                    }
                });
            }
        });
        //////////////////////////////////////以上是修改每个页面修改的地方
        //弹出修改页面调用初始化数据方法
        var findRoleName_url="role/json_role.jhtml";
        var title1="开设账号";
        var size = ['800px', '600px'];
        function initUpdate() {
            $('#myform')[0].reset();
            layer.open({
                type: 1,
                title: title1,
                area:size,
                content: $('#layuiadmin-form-useradmin'),
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                    var name='${session.om_user.companyName}';
                    $("#cname").val(name);
                }
            });

            $.ajax({
                url: findRoleName_url,
                type: "POST",
                data:{
                },
                dataType: "json",
                success: function(ret){
                    var laytpl = layui.laytpl;
                    var data = { //数据
                        "list2": ret.data
                    };
                    laytpl($("#demo2").html()).render(data, function (html) {
                        $("#roleId").html(html);
                    });
                    form.render();
                },
                error:function(jqXHR, textStatus, errorThrown){

                }
            });


            //富文本表单验证
            form.verify({
                introduce: function (value) {
                    var val = layedit.getText(index);
                    if (val.length < 6) {
                        return '内容至少6个字符'
                    }
                }
            });
        }

        function upData(data) {
            var title1="编辑账号";
            layer.open({
                type: 1,
                title: title1,
                area:size,
                content: $('#layuiadmin-form-useradmin-a'),
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
                success: function (layero, index) {
                    layer.iframeAuto(index);
                    var name='${session.om_user.companyName}';
                    $("#cname").val(name);
                }
            });

            $.ajax({
                url: findPro_url,
                type: "POST",
                data: {},
                dataType: "json",
                success: function (ret) {
                    var laytpl = layui.laytpl;
                    var rets = { //数据
                        "list": ret,
                        depart: data.proId
                    };
                    laytpl($("#demo").html()).render(rets, function (html) {
                        $("#proId1").html(html);
                    });
                    form.render();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                }
            });
            $.ajax({
                url: "greedLandAdministrator/getArea.json",
                type: "POST",
                data: {
                    departid: data.proId
                },
                dataType: "json",
                success: function (ret) {
                    var laytpl = layui.laytpl;
                    var ret2 = { //数据
                        "list2": ret
                    };
                    laytpl($("#demo1").html()).render(ret2, function (html) {
                        $("#checkBox").html(html);
                    });
                    form.render();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                }
            });
            layui.form.val("component-form-element-b", loadData(data));
        }

        function loadData(data){
            var ret = {
                "id": data.id,
                "phone": data.phone,
                "passWord": data.passWord,
                "telePhone": data.telePhone,
                "name": data.name,
                "remker": data.remker,
                "role_name":data.sysRole.sysRoleCode.role_name,
                "roleId":data.sysRole.id,
                "companyName":data.companyName
            };
            form.render();
            return ret;
        }
        form.on('select(area1)', function (data) {
            var did=data.value;
            if(data.value==""){
                layer.msg('暂无相关信息', {
                    time: 0, //不自动关闭
                    btn: [ '确认']
                });
                return false ;
            }else{
                $.ajax({
                    url: findArea_url,
                    type: "POST",
                    data:{
                        departid:did
                    },
                    dataType: "json",
                    success: function(ret){
                        var laytpl = layui.laytpl;
                        var data = { //数据
                            "list1": ret
                        };
                        laytpl($("#demo1").html()).render(data, function (html) {
                            $("#qwe").html(html);
                        });
                        form.render('checkbox');
                    },
                    error:function(jqXHR, textStatus, errorThrown){

                    }
                });
            }

        });


        var findArea_url="greedLandAdministrator/getArea.jhtml";
        form.render('select');
        form.on('select(area)', function (data) {
            var did=data.value;
            if(data.value==""){
                layer.msg('暂无相关信息', {
                    time: 0, //不自动关闭
                    btn: [ '确认']
                });
                return false ;
            }else{
                $.ajax({
                    url: findArea_url,
                    type: "POST",
                    data:{
                        departid:did
                    },
                    dataType: "json",
                    success: function(ret){
                        var laytpl = layui.laytpl;
                        var data = { //数据
                            "list1": ret
                        };
                        laytpl($("#demo1").html()).render(data, function (html) {
                            $("#xxx").html(html);
                        });
                        form.render('checkbox');
                    },
                    error:function(jqXHR, textStatus, errorThrown){

                    }
                });
            }

        });


        $.fn.ghostsf_serialize = function () {
            var a = this.serializeArray();
            var $radio = $('input[type=checkbox]', this);
            var temp = {};
            $.each($radio, function () {
                if (!temp.hasOwnProperty(this.name)) {
                    if ($("input[name='" + this.name + "']:checked").length == 0) {
                        temp[this.name] = "";
                        a.push({name: this.name, value: ""});
                    }
                }
            });
            return a;
        };

        //验证通过后提交数据到后台
        form.on('submit(component-form-element-a)', function(){
            var names = document.getElementsByName("areaId");
            var flag = false ;//标记判断是否选中一个
            for(var i=0;i<names.length;i++){
                if(names[i].checked){
                    flag = true ;
                    break ;
                }
            }
            if(!flag){
                layer.msg('请选择标段', {
                    time: 0, //不自动关闭
                    btn: [ '确认']
                });
                return false ;
            }
            var formObject = {};
            var formArray =$("#myform").ghostsf_serialize();
            $.each(formArray,function(i,item){
                formObject[item.name] = item.value;
            });
            var checkbox =[];
            $("input[name=areaId]:checked").each(function(index,itme){
                checkbox.push(itme.value)
            });
            formObject["areaId"]=checkbox.toString();
            openAccount(formObject);
            return false;
        });

        form.on('submit(component-form-element-b)', function(){
            var names = document.getElementsByName("areaId");
            var flag = false ;//标记判断是否选中一个
            for(var i=0;i<names.length;i++){
                if(names[i].checked){
                    flag = true ;
                    break ;
                }
            }
            if(!flag){
                layer.msg('请选择标段', {
                    time: 0, //不自动关闭
                    btn: [ '确认']
                });
                return false ;
            }
            var formObject = {};
            var formArray =$("#myform1").ghostsf_serialize();
            $.each(formArray,function(i,item){
                formObject[item.name] = item.value;
            });
            var checkbox =[];
            $("input[name=areaId]:checked").each(function(index,itme){
                checkbox.push(itme.value)
            });
            formObject["areaId"]=checkbox.toString();
            UpDateAccount(formObject);
            return false;
        });

        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdate();
                    break;
                case 'delete':
                    var data = checkStatus.data;
                    if (data.length > 0) {
                        //var id = new Array(); //声明数组
                        for (var i in data) {
                            var a = data[i].id;//得到复选框的值
                            postDelete(a);
                        }
                    } else {
                        layer.msg("至少选择一个进行删除");
                    }
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                upData(data);
            } else if (obj.event === 'view') {
            }else if (obj.event === 'del') {
                layer.confirm('删除以后数据将不能恢复，确定删除吗？', function(index) {
                    postDelete(data.id);
                });
            }
        });
        //表单回显，赋值
        var title2="编辑账号信息";
        var findProOne_url="system/openAccount/findProOne.jhtml";
        //ajax提交删除内容\
        window.postDelete = function (id) {
            $.ajax({
                url:delete_url,
                type: "POST",
                data: {
                    "pid": id
                },
                success: function (ret) {
                    if (ret == null) {
                        layer.msg("删除失败", {
                            icon: 5
                        });
                    } else {
                        layer.msg("删除成功", {
                            icon: 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                }
            })
        };

        //开设账户ajax
        window.openAccount = function (data) {
            var formData=new FormData();
            formData.append("data",JSON.stringify(data));
            $.ajax({
                url: openAccount_url,
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            table.reload('listReload');
                        });
                        location.replace(location.href);
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        };

        window.UpDateAccount = function (data) {
            var formData=new FormData();
            formData.append("data",JSON.stringify(data));
            $.ajax({
                url: "system/openAccount/upDateAccount.jhtml",
                type: "POST",
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            table.reload('listReload');
                        });
                        location.replace(location.href);
                    } else {
                        layer.msg(data.msg, {icon: 6});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        };
    });

</script>
</body>
</html>
